package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.domain.DictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.SysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<DictData> dictData = DICT_MAP.get(dictType);
        if (CollectionUtils.isEmpty(dictData)) {
            List<SysDictData> sysDictData = dictDataMapper.selectDictDataByType(dictType);
            DICT_MAP.put(dictType, sysDictData.parallelStream().map(s -> {
                DictData d = new DictData();
                BeanUtils.copyProperties(s, d);
                return d;
            }).collect(Collectors.toList()));
            dictData = DICT_MAP.get(dictType);
        }
        return dictData.parallelStream().map(d -> {
            SysDictData s = new SysDictData();
            BeanUtils.copyProperties(d, s);
            return s;
        }).collect(Collectors.toList());
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 通过字典ID删除字典数据信息
     *
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode) {
        DICT_MAP.clear();
        return dictDataMapper.deleteDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据
     *
     * @param dictData 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(SysDictData dictData) {
        DICT_MAP.clear();
        return dictDataMapper.deleteDictDataByIds(dictData);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData) {
        DICT_MAP.remove(dictData.getDictType());
        return dictDataMapper.insertDictData(dictData);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData) {
        DICT_MAP.remove(dictData.getDictType());
        return dictDataMapper.updateDictData(dictData);
    }

    @Override
    public Map<String, List<DictData>> selectAll(Class<?> c) {
        ReflectionUtils.doWithFields(c, field -> {
            Excel excel = field.getAnnotation(Excel.class);
            String dictType = excel.readConverterDict();
            selectDictDataByType(dictType);
        }, field -> field.isAnnotationPresent(Excel.class) && StringUtils.isNotBlank(field.getAnnotation(Excel.class).readConverterDict()));
        return DICT_MAP;
    }
}
